package com.yyq.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyq.eduservice.client.Vodclient;
import com.yyq.eduservice.entity.EduChapter;
import com.yyq.eduservice.entity.EduCourse;
import com.yyq.eduservice.entity.EduVideo;
import com.yyq.eduservice.service.EduChapterService;
import com.yyq.eduservice.service.EduCourseService;
import com.yyq.eduservice.service.EduVideoService;
import com.yyq.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author yyq
 * @since 2021-04-15
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu-video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private Vodclient vodclient;
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    //前台界面使用　　根据视频源ｉｄ
    @GetMapping("getInfoVideo/{videoSourceId}")
    public R getInfoVideo(@PathVariable String videoSourceId){
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("video_source_id",videoSourceId);
        EduVideo one = eduVideoService.getOne(wrapper);
        EduCourse courseInfo = courseService.getById(one.getCourseId());
        EduChapter chapterInfo = chapterService.getById(one.getChapterId());

        Map<String,Object> map=new HashMap<>();
        map.put("courseId",one.getCourseId());
        map.put("courseTitle",courseInfo.getTitle());
        map.put("chapterId",one.getChapterId());
        map.put("chapterTitle",chapterInfo.getTitle());
        map.put("videoTitle",one.getTitle());


        return R.ok().data(map);
    }

    //添加小结
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }
    //编辑小结
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }
    //删除小结
    @DeleteMapping("deleteVideo/{id}")
    public R deleetVideo(@PathVariable String id){
        //删除阿里云云端视频
        //获取视频ｉｄ值
        EduVideo byId = eduVideoService.getById(id);
        String videoSourceId = byId.getVideoSourceId();
        if(videoSourceId!=null)
        {
            vodclient.deleteVideo(videoSourceId);
        }

        eduVideoService.removeById(id);
        return R.ok();
    }
}

